package isisv1

import (
	"fmt"
	"topobuild/utils"

	"github.com/xuri/excelize/v2"
)

/*
设备名称	设备IP地址	ISIS进程号	NET地址	ISIS级别	设备标识符
ACR2	110.10.0.3	1	10.0000.1100.1000.0003.00	level2	110.10.0.3
ACR1	110.10.0.2	1	10.0000.1100.1000.0002.00	level2	110.10.0.2
A169A2	100.10.0.3	1	10.0000.1000.1000.0003.00	level2	100.10.0.3
A169A1	100.10.0.2	1	10.0000.1000.1000.0002.00	level2	100.10.0.2
*/

type TableIsisv1Node struct {
	DevName       string `json:"dev_name"`
	DevIp         string `json:"dev_ip"`
	IsisProcessId int    `json:"isis_process_id"`
	NetAddr       string `json:"net_addr"`
	IsisLevel     string `json:"isis_level"`
	DevId         string `json:"dev_id"`
}

// 将TableIsisv1Node中的数据,写入到指定xlsx文件的node表中
func WriteNodeXlsx(filename string, nodes []TableIsisv1Node) {
	// 如果filename已经存在,则直接打开,否则创建一个新文件
	utils.CheckFile(filename)
	xlsx, err := excelize.OpenFile(filename)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer xlsx.Close()
	// Create a new sheet.
	index, err := xlsx.NewSheet("node")
	if err != nil {
		fmt.Println(err)
		return
	}
	// Set value of a cell.
	xlsx.SetCellValue("node", "A1", "设备名称")
	xlsx.SetCellValue("node", "B1", "设备IP地址")
	xlsx.SetCellValue("node", "C1", "ISIS进程号")
	xlsx.SetCellValue("node", "D1", "NET地址")
	xlsx.SetCellValue("node", "E1", "ISIS级别")
	xlsx.SetCellValue("node", "F1", "设备标识符")
	// 设置单元格样式
	// style, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"center"}}`)
	// xlsx.SetCellStyle("dev", "A1", "G1", style)
	// 设置列宽
	// xlsx.SetColWidth("dev", "A", "G", 20)
	// 设置行高
	// Set active sheet of the workbook.
	xlsx.SetActiveSheet(index)
	// Save xlsx file by the given path.
	fmt.Printf("len(nodes): %v\n", len(nodes))
	for i, node := range nodes {
		// fmt.Printf("node: %v\n", node)
		r := i + 2
		xlsx.SetCellValue("node", fmt.Sprintf("A%v", r), node.DevName)
		xlsx.SetCellValue("node", fmt.Sprintf("B%v", r), node.DevIp)
		xlsx.SetCellValue("node", fmt.Sprintf("C%v", r), node.IsisProcessId)
		xlsx.SetCellValue("node", fmt.Sprintf("D%v", r), node.NetAddr)
		xlsx.SetCellValue("node", fmt.Sprintf("E%v", r), node.IsisLevel)
		xlsx.SetCellValue("node", fmt.Sprintf("F%v", r), node.DevId)
	}
	xlsx.Save()
}
